package com.winit.openapi.wh.outbound.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.outbound.OutboundOrderVasService;
import com.winit.oms.spi.outbound.command.order.QueryOutboundOrderVasCommand;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderValueAddedServiceVo;
import com.winit.oms.spi.vas.command.QueryVasOrderCommand;
import com.winit.oms.spi.vas.service.VasOrderServiceSnapshotService;
import com.winit.oms.spi.vas.vo.VasOrderServiceSnapshotVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.outbound.builder.OutboundOrderVasBuilder;

/**
 * @version <pre>
 * Author	  Version		 Date		 Changes
 * dinghua.an 	   1.0  		2017年11月23日 	 Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/vas")
public class OutboundOrderVasController extends BaseController {

    @Resource
    private VasOrderServiceSnapshotService vasOrderServiceSnapshotService;
    @Resource
    private OutboundOrderVasService        outboundOrderVasService;

    /**
     * 查询增值服务单列表.
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryValueAddedService")
    public String queryOrderByOrderNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryVasOrderCommand command = new QueryVasOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;
            ValidateUtil.validNotNull(jsonObj, "orderNo");
            ValidateUtil.validMaxLength(jsonObj, "orderNo", 64);
            command.setWinitOrderNo(jsonObj.getString("orderNo"));
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<VasOrderServiceSnapshotVo> vasList = vasOrderServiceSnapshotService.queryByWinitOrderNo(command);
        if (null != vasList && vasList.size() > 0) {
            List<Map<String, Object>> mapList = OutboundOrderVasBuilder.buildVasList(vasList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("valueAddedserviceList", mapList);
            responseMsg.setData(map);
        }
        return SUCCESS;
    }
    
    /**
     * 查询出库单增值服务单列表.
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryOutboundOrderVasList")
    public String queryOutboundOrderVasList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryOutboundOrderVasCommand command = new QueryOutboundOrderVasCommand();
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;
            ValidateUtil.validNotNull(jsonObj, "orderNo");
            ValidateUtil.validMaxLength(jsonObj, "orderNo", 64);
            command.setOrderNo(jsonObj.getString("orderNo"));
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        outboundOrderVasService.getValueAddedServiceVoByOrderNo(command);
        List<OutboundOrderValueAddedServiceVo> vasList = outboundOrderVasService.getValueAddedServiceVoByOrderNo(command);
        if (null != vasList && vasList.size() > 0) {
            List<Map<String, Object>> mapList = OutboundOrderVasBuilder.buildOutboundVasList(vasList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("valueAddedserviceList", mapList);
            responseMsg.setData(map);
        }
        return SUCCESS;
    }
}
